package spring.dao;

import db.domain.User;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smallframework.spring.DaoTemplate;
import sf.codegen.EntityEnhancerJavassist;
import sf.common.wrapper.Page;
import sf.spring.util.LinkedCaseInsensitiveMap;
import spring.BaseServiceTest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TemplateDAO extends BaseServiceTest {
    static {
        // new EntityEnhancer().enhanceJavassist("sf.db.domain");
        just();
    }

    //	@BeforeClass
    public static void just() {
        new EntityEnhancerJavassist().enhance("db.domain");
    }

    static Logger log = LoggerFactory.getLogger(TemplateDAO.class);

    @Resource
    private DaoTemplate dt;

    @BeforeClass
    public static void before() {
    }


    @Test
    public void testTemplate() {
        Map<String, Object> params = new HashMap<>();
        params.put("id", 1);
        List<Object> nicenames = Arrays.asList("1", "aa");
        params.put("nicenames", nicenames);
        List<User> list = dt.selectListTemplate(User.class, "queryUserByName", params);
        params.put("nicenames", new Object[]{"1", "aa"});
        long start = System.currentTimeMillis();
        list = dt.selectListTemplate(User.class, "queryUserByName", params);
        System.out.println(System.currentTimeMillis() - start + "ms -->1");
        params.put("nicenames", "1");
        start = System.currentTimeMillis();
        list = dt.selectListTemplate(User.class, "queryUserByName", params);
        System.out.println(System.currentTimeMillis() - start + "ms -->2");
        for (User u : list) {
            dt.fetchLinks(u);
        }
        start = System.currentTimeMillis();
        Page<User> page = dt.selectPageTemplate(0, 10, User.class, "queryUserByNamePage", params);
        System.out.println(System.currentTimeMillis() - start + "ms -->2");
        System.out.println(1);
        // System.out.println(list2);
    }


    @Test
    public void testMap() {
        Map<String, Object> query = new HashMap<>();
        query.put("id", 1);
        List<Object> nicenames = Arrays.asList("1", "luck");
        query.put("nicenames", nicenames);
        List<Map<String,Object>> user = dt.selectTemplate("queryUserByName", query);
        System.out.println(user);
    }

    @Test
    public void testOne() {
        Map<String, Object> query = new HashMap<>();
        query.put("id", 1);
        List<Object> nicenames = Arrays.asList("1", "luck");
        query.put("nicenames", nicenames);
        User user = dt.selectOneTemplate(User.class, "queryUserByName", query);
        System.out.println(user);
    }

    @Test
    public void testList() {
        Map<String, Object> query = new HashMap<>();
        query.put("id", 1);
        List<Object> nicenames = Arrays.asList("1", "aa");
        query.put("nicenames", nicenames);
        List<User> list = dt.selectListTemplate(User.class, "queryUserByName", query);
        System.out.println(list);
    }

    @Test
    public void testPage() {
        Map<String, Object> query = new HashMap<>();
        query.put("id", 1);
        List<Object> nicenames = Arrays.asList("1", 2);
        query.put("nicenames", nicenames);
        Page<User> page = dt.selectPageTemplate(0, 10, User.class, "queryUserByNamePage", query);
        System.out.println(page);
    }

    @Test
    public void testBatchInsert() {
        Map<String, Object> map = new HashMap<>();
        map.put("loginName", "admin");
        map.put("activationKey", "as");
        map.put("deleted", false);
        map.put("dataValid", true);

        Map<String, Object> map2 = new HashMap<>();
        map2.put("loginName", "admin");
        map2.put("activationKey", "as");
        map2.put("deleted", false);
        map2.put("dataValid", true);
        List<Map<String, Object>> keyValues = new ArrayList<>();
        dt.executeBatchTemplate("insertUser", Arrays.asList(map, map2), false, 100, Arrays.asList("id"), keyValues);
        System.out.println(keyValues);

    }

    @Test
    public void testInsert() {
        Map<String, Object> map = new HashMap<>();
        map.put("loginName", "admin");
        map.put("activationKey", "as");
        map.put("deleted", false);
        map.put("dataValid", true);
        Map<String, Object> keyValues = new LinkedCaseInsensitiveMap<>();
        int i = dt.insertTemplate("insertUser", map, Arrays.asList("id"), keyValues);
        if (i > 0) {
            System.out.println(keyValues);
        }

    }

    @Test
    public void testUpdate() {
        Map<String, Object> map = new HashMap<>();
        map.put("activationKey", "as");
        map.put("id", 3);
        dt.executeTemplate("updateUser", map);
    }

    @Test
    public void testDelete() {
        Map<String, Object> map = new HashMap<>();
        map.put("id", 1);
        dt.executeTemplate("deleteUser", map);
    }

}
